## Morocco

rm(list=ls())
library(foreign)

# Labor
mydata1 <- read.table("E:/poor_labor.txt")
attach(mydata1)
mydata2 <- read.table("E:/rich_labor.txt")
attach(mydata2)

matcoeflaborpoor <- matrix(0,4,4)

for (i in 1:4) {

  matcoeflaborpoor[i,1] <- i
  matcoeflaborpoor[i,2] <- mydata1[i,1]
  matcoeflaborpoor[i,3] <- mydata1[i,1]-1.96*mydata1[i,2]
  matcoeflaborpoor[i,4] <- mydata1[i,1]+1.96*mydata1[i,2]

  }
  
matcoeflaborrich <- matrix(0,4,4)

for (i in 1:4) {

  matcoeflaborrich[i,1] <- i
  matcoeflaborrich[i,2] <- mydata2[i,1]
  matcoeflaborrich[i,3] <- mydata2[i,1]-1.96*mydata2[i,2]
  matcoeflaborrich[i,4] <- mydata2[i,1]+1.96*mydata2[i,2]

  }

# Culture

mydata3 <- read.table("E:/poor_culture.txt")
attach(mydata3)
mydata4 <- read.table("E:/rich_culture.txt")
attach(mydata4)

matcoefculturepoor <- matrix(0,4,4)

for (i in 1:4) {

  matcoefculturepoor[i,1] <- i
  matcoefculturepoor[i,2] <- mydata3[i,1]
  matcoefculturepoor[i,3] <- mydata3[i,1]-1.96*mydata3[i,2]
  matcoefculturepoor[i,4] <- mydata3[i,1]+1.96*mydata3[i,2]

  }
  
matcoefculturerich <- matrix(0,4,4)

for (i in 1:4) {

  matcoefculturerich[i,1] <- i
  matcoefculturerich[i,2] <- mydata4[i,1]
  matcoefculturerich[i,3] <- mydata4[i,1]-1.96*mydata4[i,2]
  matcoefculturerich[i,4] <- mydata4[i,1]+1.96*mydata4[i,2]

  }

# Fiscal

mydata5 <- read.table("E:/poor_fiscal.txt")
attach(mydata5)
mydata6 <- read.table("E:/rich_fiscal.txt")
attach(mydata6)

matcoeffiscalpoor <- matrix(0,4,4)

for (i in 1:4) {

  matcoeffiscalpoor[i,1] <- i
  matcoeffiscalpoor[i,2] <- mydata5[i,1]
  matcoeffiscalpoor[i,3] <- mydata5[i,1]-1.96*mydata5[i,2]
  matcoeffiscalpoor[i,4] <- mydata5[i,1]+1.96*mydata5[i,2]

  }
  
matcoeffiscalrich <- matrix(0,4,4)

for (i in 1:4) {

  matcoeffiscalrich[i,1] <- i
  matcoeffiscalrich[i,2] <- mydata6[i,1]
  matcoeffiscalrich[i,3] <- mydata6[i,1]-1.96*mydata6[i,2]
  matcoeffiscalrich[i,4] <- mydata6[i,1]+1.96*mydata6[i,2]

  }

# Security
  
mydata7 <- read.table("E:/poor_security.txt")
attach(mydata7)
mydata8 <- read.table("E:/rich_security.txt")
attach(mydata8)

matcoefsecuritypoor <- matrix(0,4,4)

for (i in 1:4) {

  matcoefsecuritypoor[i,1] <- i
  matcoefsecuritypoor[i,2] <- mydata7[i,1]
  matcoefsecuritypoor[i,3] <- mydata7[i,1]-1.96*mydata7[i,2]
  matcoefsecuritypoor[i,4] <- mydata7[i,1]+1.96*mydata7[i,2]

  }
  
matcoefsecurityrich <- matrix(0,4,4)

for (i in 1:4) {

  matcoefsecurityrich[i,1] <- i
  matcoefsecurityrich[i,2] <- mydata8[i,1]
  matcoefsecurityrich[i,3] <- mydata8[i,1]-1.96*mydata8[i,2]
  matcoefsecurityrich[i,4] <- mydata8[i,1]+1.96*mydata8[i,2]

  }





### Plot

# Labor market threat

pdf("appendix_labor.pdf")
par(mfrow=c(2,2))
par(mar=c(5,5,5,3))

plot(matcoeflaborpoor[,1], matcoeflaborpoor[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of labor market threat on \n greater opposition to African migrants", main="Under-developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoeflaborpoor[1,1],matcoeflaborpoor[1,3],matcoeflaborpoor[1,1],matcoeflaborpoor[1,4], angle=0)
    arrows(matcoeflaborpoor[2,1],matcoeflaborpoor[2,3],matcoeflaborpoor[2,1],matcoeflaborpoor[2,4], angle=0)
    arrows(matcoeflaborpoor[3,1],matcoeflaborpoor[3,3],matcoeflaborpoor[3,1],matcoeflaborpoor[3,4], angle=0)
    arrows(matcoeflaborpoor[4,1],matcoeflaborpoor[4,3],matcoeflaborpoor[4,1],matcoeflaborpoor[4,4], angle=0)    
    
plot(matcoeflaborrich[,1], matcoeflaborrich[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of labor market threat on \n greater opposition to African migrants", main="Developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoeflaborrich[1,1],matcoeflaborrich[1,3],matcoeflaborrich[1,1],matcoeflaborrich[1,4], angle=0)
    arrows(matcoeflaborrich[2,1],matcoeflaborrich[2,3],matcoeflaborrich[2,1],matcoeflaborrich[2,4], angle=0)
    arrows(matcoeflaborrich[3,1],matcoeflaborrich[3,3],matcoeflaborrich[3,1],matcoeflaborrich[3,4], angle=0)
    arrows(matcoeflaborrich[4,1],matcoeflaborrich[4,3],matcoeflaborrich[4,1],matcoeflaborrich[4,4], angle=0) 

    dev.off()


# Fiscal threat

pdf("appendix_fiscal.pdf")
par(mfrow=c(2,2))
par(mar=c(5,5,5,3))

plot(matcoeffiscalpoor[,1], matcoeffiscalpoor[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of fiscal threat on \n greater opposition to African migrants", main="Under-developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoeffiscalpoor[1,1],matcoeffiscalpoor[1,3],matcoeffiscalpoor[1,1],matcoeffiscalpoor[1,4], angle=0)
    arrows(matcoeffiscalpoor[2,1],matcoeffiscalpoor[2,3],matcoeffiscalpoor[2,1],matcoeffiscalpoor[2,4], angle=0)
    arrows(matcoeffiscalpoor[3,1],matcoeffiscalpoor[3,3],matcoeffiscalpoor[3,1],matcoeffiscalpoor[3,4], angle=0)
    arrows(matcoeffiscalpoor[4,1],matcoeffiscalpoor[4,3],matcoeffiscalpoor[4,1],matcoeffiscalpoor[4,4], angle=0)    
    
plot(matcoeffiscalrich[,1], matcoeffiscalrich[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of fiscal threat on \n greater opposition to African migrants", main="Developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoeffiscalrich[1,1],matcoeffiscalrich[1,3],matcoeffiscalrich[1,1],matcoeffiscalrich[1,4], angle=0)
    arrows(matcoeffiscalrich[2,1],matcoeffiscalrich[2,3],matcoeffiscalrich[2,1],matcoeffiscalrich[2,4], angle=0)
    arrows(matcoeffiscalrich[3,1],matcoeffiscalrich[3,3],matcoeffiscalrich[3,1],matcoeffiscalrich[3,4], angle=0)
    arrows(matcoeffiscalrich[4,1],matcoeffiscalrich[4,3],matcoeffiscalrich[4,1],matcoeffiscalrich[4,4], angle=0) 

    dev.off()


# Security threat

pdf("appendix_security.pdf")
par(mfrow=c(2,2))
par(mar=c(5,5,5,3))

plot(matcoefsecuritypoor[,1], matcoefsecuritypoor[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of security threat on \n greater opposition to African migrants", main="Under-developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoefsecuritypoor[1,1],matcoefsecuritypoor[1,3],matcoefsecuritypoor[1,1],matcoefsecuritypoor[1,4], angle=0)
    arrows(matcoefsecuritypoor[2,1],matcoefsecuritypoor[2,3],matcoefsecuritypoor[2,1],matcoefsecuritypoor[2,4], angle=0)
    arrows(matcoefsecuritypoor[3,1],matcoefsecuritypoor[3,3],matcoefsecuritypoor[3,1],matcoefsecuritypoor[3,4], angle=0)
    arrows(matcoefsecuritypoor[4,1],matcoefsecuritypoor[4,3],matcoefsecuritypoor[4,1],matcoefsecuritypoor[4,4], angle=0)    
    
plot(matcoefsecurityrich[,1], matcoefsecurityrich[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of security threat on \n greater opposition to African migrants", main="Developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoefsecurityrich[1,1],matcoefsecurityrich[1,3],matcoefsecurityrich[1,1],matcoefsecurityrich[1,4], angle=0)
    arrows(matcoefsecurityrich[2,1],matcoefsecurityrich[2,3],matcoefsecurityrich[2,1],matcoefsecurityrich[2,4], angle=0)
    arrows(matcoefsecurityrich[3,1],matcoefsecurityrich[3,3],matcoefsecurityrich[3,1],matcoefsecurityrich[3,4], angle=0)
    arrows(matcoefsecurityrich[4,1],matcoefsecurityrich[4,3],matcoefsecurityrich[4,1],matcoefsecurityrich[4,4], angle=0) 

    dev.off()


# Cultural threat

pdf("appendix_culture.pdf")
par(mfrow=c(2,2))
par(mar=c(5,5,5,3))

plot(matcoefculturepoor[,1], matcoefculturepoor[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of cultural threat on \n greater opposition to African migrants", main="Under-developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=4)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoefculturepoor[1,1],matcoefculturepoor[1,3],matcoefculturepoor[1,1],matcoefculturepoor[1,4], angle=0)
    arrows(matcoefculturepoor[2,1],matcoefculturepoor[2,3],matcoefculturepoor[2,1],matcoefculturepoor[2,4], angle=0)
    arrows(matcoefculturepoor[3,1],matcoefculturepoor[3,3],matcoefculturepoor[3,1],matcoefculturepoor[3,4], angle=0)
    arrows(matcoefculturepoor[4,1],matcoefculturepoor[4,3],matcoefculturepoor[4,1],matcoefculturepoor[4,4], angle=0)    
    
plot(matcoefculturerich[,1], matcoefculturerich[,2], pch=1, ylim=c(-2,2), xlim=c(1,4), xaxt="n", yaxt="n", xlab="Income sufficiency", ylab="Effect of cultural threat on \n greater opposition to African migrants", main="Developed communities", cex.lab=1.0, cex.main=1.2)
    customlabels<-seq(-2,2,by=2)
    axis(2,at=customlabels,labels=customlabels)
    customlabels<-seq(1,4,by=1)
    axis(1,at=customlabels,labels=customlabels)
    abline(h=0,lty=6)
    arrows(matcoefculturerich[1,1],matcoefculturerich[1,3],matcoefculturerich[1,1],matcoefculturerich[1,4], angle=0)
    arrows(matcoefculturerich[2,1],matcoefculturerich[2,3],matcoefculturerich[2,1],matcoefculturerich[2,4], angle=0)
    arrows(matcoefculturerich[3,1],matcoefculturerich[3,3],matcoefculturerich[3,1],matcoefculturerich[3,4], angle=0)
    arrows(matcoefculturerich[4,1],matcoefculturerich[4,3],matcoefculturerich[4,1],matcoefculturerich[4,4], angle=0) 

    dev.off()
    